package com.lsh.wechat;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;

/**
 * @author ：LiuShihao
 * @date ：Created in 2020/9/17 2:02 下午
 * @desc ：
 */
public class WeChatClient1 {

    private static final String HOST ="127.0.0.1";
    private static final int PORT = 6667;
    private SocketChannel socketChannel;
    private Selector selector;
    private String username;

    public WeChatClient1() throws IOException {
        selector = Selector.open();
        socketChannel = socketChannel.open(new InetSocketAddress(HOST,PORT));
        socketChannel.configureBlocking(false);
        //将channle注册到selector
        socketChannel.register(selector, SelectionKey.OP_READ);
        username = socketChannel.getLocalAddress().toString().substring(1);
        System.out.println(username+"is OK...");

    }

    /**
     * 向服务器发送消息
     */
    public void snedInfo(String info){
        info = username + "说："+info;
        try {
            socketChannel.write(ByteBuffer.wrap(info.getBytes()));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void readInfo(){
        try {
            int select = selector.select(2000);
            if (select>0){
                //有可以用的通道
                Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
                while(iterator.hasNext()){
                    SelectionKey key = iterator.next();
                    if (key.isReadable()){
                        //得到相关通道
                        SocketChannel channel = (SocketChannel)key.channel();
                        //得到一个buffer
                        ByteBuffer buffer = ByteBuffer.allocate(1024);
                        //将通道中的数据读取到缓冲区
                        channel.read(buffer);
                        //把读到缓冲区的数据 转成字符串
                        String msg = new String(buffer.array());
                        System.out.println(msg.trim());
                    }
                    iterator.remove();
                }
            }else {
//                System.out.println("没有可用的通道");

            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }
    public static void main(String[] args) throws IOException {

        WeChatClient1 weChatClient = new WeChatClient1();

        new Thread(()->{
            while (true){
                weChatClient.readInfo();
                    try {
                        Thread.currentThread(). sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }
        }).start();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()){
            String s = scanner.nextLine();
            weChatClient.snedInfo(s.trim());
        }

    }
}
